using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {
    #region public params
    //跳跃的冷却时间
    public float jumpCoolTime = 0.5f;

    //跳跃的力度
    public float jumpForce = 1f;
    #endregion
    #region  private params
    //用来计时的变量
    private float jumpTimer;
    //rigidbody
    private Rigidbody2D rb;
    private Vector3 initPos;
    #endregion

    void Awake () {
        this.rb = this.GetComponent<Rigidbody2D> ();
    }

    // Start is called before the first frame update
    void Start () {
        this.jumpTimer = this.jumpCoolTime;
        this.initPos = this.transform.position;
        Physics2D.queriesStartInColliders = false; //设置射线不要检测到自身
    }

    // Update is called once per frame
    void Update () {
        this.jumpTimer -= Time.deltaTime;
        // Debug.Log ("是否检测到地面：" + GameManager.checkIsGround (this.transform.positionVector3.down * 0.5f));
        if (this.jumpTimer < 0 && GameManager.checkIsGround (this.transform.position + Vector3.down * 0.3f)) {
            this.rb.AddForce (Vector3.up * jumpForce * 100);
            this.jumpTimer = this.jumpCoolTime;
        }
    }

    public void reset(){
        this.rb.velocity = Vector2.zero;
        this.transform.position = this.initPos;
    }
}